package com.spic.system.controller;

import com.google.common.collect.Lists;
import com.spic.common.core.utils.poi.ExcelUtil;
import com.spic.common.core.web.controller.BaseController;
import com.spic.common.core.web.domain.AjaxResult;
import com.spic.common.core.web.page.TableDataInfo;
import com.spic.common.datascope.annotation.DataScope;
import com.spic.common.log.annotation.Log;
import com.spic.common.log.enums.BusinessType;
import com.spic.common.security.annotation.PreAuthorize;
import com.spic.system.domain.SysNoticeInfo;
import com.spic.system.service.ISysNoticeInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 我的消息Controller
 *
 * @author spic
 * @date 2021-11-04
 */
@RestController
@RequestMapping("/noticeinfo")
public class SysNoticeInfoController extends BaseController
{
    @Autowired
    private ISysNoticeInfoService sysNoticeInfoService;

    /**
     * 查询我的消息列表
     */
    @PreAuthorize(hasPermi = "system:noticeinfo:list")
    @GetMapping("/list")
    @DataScope(deptAlias = "d", userAlias = "u", menuPath = "system/noticeinfo/index")
    public TableDataInfo list(SysNoticeInfo sysNoticeInfo)
    {
        startPage();
        List<SysNoticeInfo> list = sysNoticeInfoService.selectSysNoticeInfoList(sysNoticeInfo);
        return getDataTable(list);
    }

    /**
     * 导出我的消息列表
     */
    @PreAuthorize(hasPermi = "system:noticeinfo:export")
    @Log(title = "我的消息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysNoticeInfo sysNoticeInfo) throws IOException
    {
        List<SysNoticeInfo> list = sysNoticeInfoService.selectSysNoticeInfoList(sysNoticeInfo);
        ExcelUtil<SysNoticeInfo> util = new ExcelUtil<SysNoticeInfo>(SysNoticeInfo.class);
        util.exportExcel(response, list, "我的消息数据");
    }

    /**
     * 获取我的消息详细信息
     */
    @PreAuthorize(hasPermi = "system:noticeinfo:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(sysNoticeInfoService.selectSysNoticeInfoById(id));
    }

    /**
     * 新增我的消息
     */
    @PostMapping
    public AjaxResult add(@RequestBody SysNoticeInfo sysNoticeInfo)
    {
        return toAjax(sysNoticeInfoService.insertSysNoticeInfo(Lists.newArrayList(sysNoticeInfo)));
    }

    /**
     * 批量新增我的消息
     */
    @PostMapping("/batch")
    public AjaxResult batchAdd(@RequestBody List<SysNoticeInfo> sysNoticeInfo)
    {
        return toAjax(sysNoticeInfoService.insertSysNoticeInfo(sysNoticeInfo));
    }


    /**
     * 修改我的消息
     */
    @PreAuthorize(hasPermi = "system:noticeinfo:edit")
    @Log(title = "我的消息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysNoticeInfo sysNoticeInfo)
    {
        return toAjax(sysNoticeInfoService.updateSysNoticeInfo(sysNoticeInfo));
    }

    /**
     * 删除我的消息
     */
    @PreAuthorize(hasPermi = "system:noticeinfo:remove")
    @Log(title = "我的消息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(sysNoticeInfoService.deleteSysNoticeInfoByIds(ids));
    }
    @GetMapping(value = "/readCount")
    public AjaxResult selectIsReadCount(){
        return AjaxResult.success(sysNoticeInfoService.selectIsReadCount());
    }
}
